bitkeeper revision 1.1159.238.1 (4200c8d8KsGlaM3w6o3y4GHhK1jKjg)
authoriap10@labyrinth.cl.cam.ac.uk <iap10@labyrinth.cl.cam.ac.uk>
Wed, 2 Feb 2005 12:34:32 +0000 (12:34 +0000)
committeriap10@labyrinth.cl.cam.ac.uk <iap10@labyrinth.cl.cam.ac.uk>
Wed, 2 Feb 2005 12:34:32 +0000 (12:34 +0000)
A typesafe allocator submitted by Rusty Russel with trivial renames by me.
Signed-off-by: Rusty Russel <rusty@rustcorp.com.au> (authored)
Signed-off-by: ian.pratt@cl.cam.ac.uk
17 files changed:
xen/arch/x86/irq.c
xen/arch/x86/mtrr/generic.c
xen/arch/x86/mtrr/main.c
xen/arch/x86/shadow.c
xen/arch/x86/smpboot.c
xen/common/ac_timer.c
xen/common/dom0_ops.c
xen/common/domain.c
xen/common/event_channel.c
xen/common/grant_table.c
xen/common/physdev.c
xen/common/resource.c
xen/common/sched_atropos.c
xen/common/sched_bvt.c
xen/drivers/pci/pci.c
xen/drivers/pci/setup-res.c
xen/include/xen/slab.h

index a11ddd3f726924010139337849bee44a33b828b5..81b8e37d31ac92ce762f7e9ca48d09d8219748f3 100644 (file)
@@ -260,7 +260,7 @@ int pirq_guest_bind(struct exec_domain *ed, int irq, int will_share)
             goto out;
         }
 
-        action = xmalloc(sizeof(irq_guest_action_t));
+        action = xmalloc(irq_guest_action_t);
         if ( (desc->action = (struct irqaction *)action) == NULL )
         {
             DPRINTK("Cannot bind IRQ %d to guest. Out of memory.\n", irq);
index 93d35350a7a9280ebf49099ef39b3c4144ff3524..6f8d104443a03099f9067b01a158c89bc1f141cb 100644 (file)
@@ -52,7 +52,8 @@ void __init get_mtrr_state(void)
        unsigned lo, dummy;
 
        if (!mtrr_state.var_ranges) {
-               mtrr_state.var_ranges = xmalloc(num_var_ranges * sizeof (struct mtrr_var_range));
+               mtrr_state.var_ranges = xmalloc_array(struct mtrr_var_range,
+                                                 num_var_ranges);
                if (!mtrr_state.var_ranges)
                        return;
        } 
index bd1ae5d05450fa9bedb186690ff9c73319a95c98..257240b1d1053d873effcd92aa69c80b89e4c3e8 100644 (file)
@@ -136,8 +136,7 @@ static void __init init_table(void)
        int i, max;
 
        max = num_var_ranges;
-       if ((usage_table = xmalloc(max * sizeof *usage_table))
-           == NULL) {
+       if ((usage_table = xmalloc_array(unsigned int, max)) == NULL) {
                printk(KERN_ERR "mtrr: could not allocate\n");
                return;
        }
index 0245a05e2789f7119daa90bb9413b969d6d33010..7fd1bbca52dc4361914c54d6327ce8178a6c87e5 100644 (file)
@@ -176,8 +176,7 @@ int shadow_mode_enable(struct domain *p, unsigned int mode)
 {
     struct mm_struct *m = &p->exec_domain[0]->mm;
 
-    m->shadow_ht = xmalloc(
-        shadow_ht_buckets * sizeof(struct shadow_status));
+    m->shadow_ht = xmalloc_array(struct shadow_status, shadow_ht_buckets);
     if ( m->shadow_ht == NULL )
         goto nomem;
     memset(m->shadow_ht, 0, shadow_ht_buckets * sizeof(struct shadow_status));
index 5609a118ed02696113e95cf9115933dcc746a337..7a98fd8ae58b93d1d74a883be925dd4fbefb98b6 100644 (file)
@@ -409,7 +409,7 @@ void __init start_secondary(void)
      * At this point, boot CPU has fully initialised the IDT. It is
      * now safe to make ourselves a private copy.
      */
-    idt_tables[cpu] = xmalloc(IDT_ENTRIES*8);
+    idt_tables[cpu] = xmalloc_array(struct desc_struct, IDT_ENTRIES);
     memcpy(idt_tables[cpu], idt_table, IDT_ENTRIES*8);
     *(unsigned short *)(&idt_load[0]) = (IDT_ENTRIES*8)-1;
     *(unsigned long  *)(&idt_load[2]) = (unsigned long)idt_tables[cpu];
index a33498090b90200af06403fb4064c85df7452cd0..6896e9945a39ed66fb44198c1d88d1f6e9efc03f 100644 (file)
@@ -130,7 +130,7 @@ static int add_entry(struct ac_timer **heap, struct ac_timer *t)
     if ( unlikely(sz == GET_HEAP_LIMIT(heap)) )
     {
         int i, limit = (GET_HEAP_LIMIT(heap)+1) << 1;
-        struct ac_timer **new_heap = xmalloc(limit*sizeof(struct ac_timer *));
+        struct ac_timer **new_heap = xmalloc_array(struct ac_timer *, limit);
         if ( new_heap == NULL ) BUG();
         memcpy(new_heap, heap, (limit>>1)*sizeof(struct ac_timer *));
         for ( i = 0; i < smp_num_cpus; i++ )
@@ -278,8 +278,7 @@ void __init ac_timer_init(void)
 
     for ( i = 0; i < smp_num_cpus; i++ )
     {
-        ac_timers[i].heap = xmalloc(
-            (DEFAULT_HEAP_LIMIT+1) * sizeof(struct ac_timer *));
+        ac_timers[i].heap = xmalloc_array(struct ac_timer *, DEFAULT_HEAP_LIMIT+1);
         if ( ac_timers[i].heap == NULL ) BUG();
         SET_HEAP_SIZE(ac_timers[i].heap, 0);
         SET_HEAP_LIMIT(ac_timers[i].heap, DEFAULT_HEAP_LIMIT);
index 6d0cbe911de52834f76d80074c20e29de8e81a4c..4611d1f2fe199c8bc7b8084d285e8decbdec85ab 100644 (file)
@@ -383,7 +383,7 @@ long do_dom0_op(dom0_op_t *u_dom0_op)
 
         if ( op->u.getdomaininfo.ctxt != NULL )
         {
-            if ( (c = xmalloc(sizeof(*c))) == NULL )
+            if ( (c = xmalloc(full_execution_context_t)) == NULL )
             {
                 ret = -ENOMEM;
                 put_domain(d);
index 8709630cd4daf5a0e0b6fe0c117f4f26e2de5a1e..37e23fc1a6681f2cb9ebea0ee95160df33019a6b 100644 (file)
@@ -264,7 +264,7 @@ int final_setup_guestos(struct domain *p, dom0_builddomain_t *builddomain)
     int rc = 0;
     full_execution_context_t *c;
 
-    if ( (c = xmalloc(sizeof(*c))) == NULL )
+    if ( (c = xmalloc(full_execution_context_t)) == NULL )
         return -ENOMEM;
 
     if ( test_bit(DF_CONSTRUCTED, &p->d_flags) )
@@ -311,7 +311,7 @@ long do_boot_vcpu(unsigned long vcpu, full_execution_context_t *ctxt)
     if ( alloc_exec_domain_struct(d, vcpu) == NULL )
         return -ENOMEM;
 
-    if ( (c = xmalloc(sizeof(*c))) == NULL )
+    if ( (c = xmalloc(full_execution_context_t)) == NULL )
     {
         rc = -ENOMEM;
         goto out;
index bc9a92052d8a154741c5adc75053c22cff835bd6..304172ba265077b2a30d85e5cf66045982d69099 100644 (file)
@@ -54,7 +54,7 @@ static int get_free_port(struct exec_domain *ed)
         else
             max = port + EVENT_CHANNELS_SPREAD;
         
-        chn = xmalloc(max * sizeof(event_channel_t));
+        chn = xmalloc_array(event_channel_t, max);
         if ( unlikely(chn == NULL) )
             return -ENOMEM;
 
index d2aaeec427757d83c9eca215d941df62b8136b4f..e6a76c9dd67bea60f9f1d04409e7095a033ad2ce 100644 (file)
@@ -565,7 +565,7 @@ grant_table_create(
     grant_table_t *t;
     int            i;
 
-    if ( (t = xmalloc(sizeof(*t))) == NULL )
+    if ( (t = xmalloc(grant_table_t)) == NULL )
         goto no_mem;
 
     /* Simple stuff. */
@@ -573,8 +573,8 @@ grant_table_create(
     spin_lock_init(&t->lock);
 
     /* Active grant table. */
-    if ( (t->active = xmalloc(sizeof(active_grant_entry_t) * 
-                              NR_GRANT_ENTRIES)) == NULL )
+    if ( (t->active = xmalloc_array(active_grant_entry_t, NR_GRANT_ENTRIES))
+        == NULL )
         goto no_mem;
     memset(t->active, 0, sizeof(active_grant_entry_t) * NR_GRANT_ENTRIES);
 
index c815ee4b326bac73ce47765bc9af812b806a78c3..f4d35e3c8bdcfdcbb3fd96d5a116f2ac3ced1fed 100644 (file)
@@ -98,7 +98,7 @@ static void add_dev_to_task(struct domain *p,
         return;
     }
 
-    if ( (pdev = xmalloc(sizeof(phys_dev_t))) == NULL )
+    if ( (pdev = xmalloc(phys_dev_t)) == NULL )
     {
         INFO("Error allocating pdev structure.\n");
         return;
@@ -174,7 +174,7 @@ int physdev_pci_access_modify(
 
     if ( ed->thread.io_bitmap == NULL )
     {
-        if ( (ed->thread.io_bitmap = xmalloc(IOBMP_BYTES)) == NULL )
+        if ( (ed->thread.io_bitmap = xmalloc_array(u8, IOBMP_BYTES)) == NULL )
         {
             rc = -ENOMEM;
             goto out;
@@ -765,7 +765,7 @@ void physdev_init_dom0(struct domain *p)
         if ( (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) &&
              (dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) )
             continue;
-        pdev = xmalloc(sizeof(phys_dev_t));
+        pdev = xmalloc(phys_dev_t);
         pdev->dev = dev;
         pdev->flags = ACC_WRITE;
         pdev->state = 0;
index 8df57772a676fbbdfaa8dbfbbba7f43c0a10dc41..0f479f068b4a2cf9926ecb2224dbd48216c92c24 100644 (file)
@@ -220,7 +220,7 @@ int allocate_resource(struct resource *root, struct resource *new,
  */
 struct resource * __request_region(struct resource *parent, unsigned long start, unsigned long n, const char *name)
 {
-       struct resource *res = xmalloc(sizeof(*res));
+       struct resource *res = xmalloc(struct resource);
 
        if (res) {
                memset(res, 0, sizeof(*res));
index b5901f2397a19d3bbd904fde1ae492343e71a7e0..32c0090fbb9e753fbbefee4f958fbb0c6c674dd9 100644 (file)
@@ -173,7 +173,7 @@ static int at_alloc_task(struct domain *p)
 {
     ASSERT(p != NULL);
     
-    p->sched_priv = xmem_cache_alloc(dom_info_cache);
+    p->sched_priv = xmalloc(struct at_dom_info);
     if ( p->sched_priv == NULL )
         return -1;
     
index 8e292432a8fd96c216731717d05adb5c83fb42cf..59b70ecfc6b511e91d7cc85b4376acd14df69602 100644 (file)
@@ -557,7 +557,7 @@ int bvt_init_scheduler()
 
     for ( i = 0; i < NR_CPUS; i++ )
     {
-        schedule_data[i].sched_priv = xmalloc(sizeof(struct bvt_cpu_info));
+        schedule_data[i].sched_priv = xmalloc(struct bvt_cpu_info);
        
         if ( schedule_data[i].sched_priv == NULL )
         {
index 50a4ebb5e07e91951d46fe15b73ef4970cfacf1c..c3c25b1dba72625005b40de63a525f25eacecc53 100644 (file)
@@ -1126,7 +1126,7 @@ static struct pci_bus * __devinit pci_alloc_bus(void)
 {
        struct pci_bus *b;
 
-       b = xmalloc(sizeof(*b));
+       b = xmalloc(struct pci_bus);
        if (b) {
                memset(b, 0, sizeof(*b));
                INIT_LIST_HEAD(&b->children);
@@ -1351,7 +1351,7 @@ struct pci_dev * __devinit pci_scan_device(struct pci_dev *temp)
        if (l == 0xffffffff || l == 0x00000000 || l == 0x0000ffff || l == 0xffff0000)
                return NULL;
 
-       dev = xmalloc(sizeof(*dev));
+       dev = xmalloc(struct pci_dev);
        if (!dev)
                return NULL;
 
@@ -1431,7 +1431,7 @@ unsigned int __devinit pci_do_scan_bus(struct pci_bus *bus)
        max = bus->secondary;
 
        /* Create a device template */
-       dev0 = xmalloc(sizeof(struct pci_dev));
+       dev0 = xmalloc(struct pci_dev);
        if(!dev0) {
          panic("Out of memory scanning PCI bus!\n");
        }
index 3435b2ac9c17ef05a972065c5a8ddd3a39b5e861..59652dbcc7a906d4df4799152245566f97501a68 100644 (file)
@@ -171,10 +171,10 @@ pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
                                        ln->res->start;
                        }
                        if (r_align > align) {
-                               tmp = xmalloc(sizeof(*tmp));
+                               tmp = xmalloc(struct resource_list);
                                if (!tmp)
                                        panic("pdev_sort_resources(): "
-                                             "xmalloc() failed!\n");
+                                             "malloc() failed!\n");
                                tmp->next = ln;
                                tmp->res = r;
                                tmp->dev = dev;
index 692b3b63f3c9526e1bdd81b382eb237e50422729..e08d02b0855dc6b0b43454d0b1385e9b66eb17e5 100644 (file)
@@ -18,6 +18,7 @@ typedef struct xmem_cache_s xmem_cache_t;
 
 #include <xen/mm.h>
 #include <xen/cache.h>
+#include <xen/types.h>
 
 /* Flags to pass to xmem_cache_create(). */
 /* NB. The first 3 are only valid when built with SLAB_DEBUG_SUPPORT. */
@@ -52,6 +53,17 @@ extern int xmem_cache_reap(void);
 
 extern void dump_slabinfo();
 
+/* Nicely typesafe for you. */
+#define xmalloc(type) ((type *)xmalloc(sizeof(type)))
+#define xmalloc_array(type, num) ((type *)xmalloc_array(sizeof(type), (num)))
+
+static inline void *xmalloc_array(size_t size, size_t num)
+{
+       /* Check for overflow. */
+       if (size && num > UINT_MAX / size)
+               return NULL;
+       return xmalloc(size * num);
+}
 #endif /* __ARCH_HAS_SLAB_ALLOCATOR */
 
 #endif /* __SLAB_H__ */